const app = getApp()
Component({
    properties: {},
    data: {
        token:'',
        checkedLED: true,
        checkedMotor: true,
        LEDstatus: '开',
        Motorstatus: '开',
    },

    //生命周期函数
    lifetimes: {
        attached: function () {
            this.setToken();
            if (this.data.token != '') {
                this.getLED();
                this.getMotor();
                this.data.timer = setInterval(() => {
                    this.getLED();
                    this.getMotor();
                }, 5000);
            } else {
                // wx.showToast({
                //     title: '请先登录',
                //     icon: 'none',
                //     duration: 2000
                // })
            }
        }
    },

    methods: {
        getLED() {
            wx.request({
                url: 'http://43.143.141.18:8080/api/v1/'+this.data.token+'/attributes',
                method: 'GET',
                success: (res) => {
                    // console.log(res)
                    if (res.data.client.power_LED == 1) {
                        this.setData({
                            checkedLED: true,
                            LEDstatus: '开',
                        })
                    } else {
                        this.setData({
                            checkedLED: false,
                            LEDstatus: '关',
                        })
                    }
                }
            })
        },

        getMotor() {
            wx.request({
                url: 'http://43.143.141.18:8080/api/v1/3U0fClbRjIwu5c5QxVkP/attributes',
                method: 'GET',
                success: (res) => {
                    // console.log(res)
                    if (res.data.client.songSwitch == 1) {
                        this.setData({
                            checkedMotor: true,
                            Motorstatus: '开',
                        })
                    } else {
                        this.setData({
                            checkedMotor: false,
                            Motorstatus: '关',
                        })
                    }
                }
            })
        },





        changeRemoteLED(state) {
            wx.request({
                url: 'http://43.143.141.18:8080/api/v1/3U0fClbRjIwu5c5QxVkP/attributes',
                method: 'POST',
                data: {
                    "power_LED":state,
                },
                success: (res) => {
                    console.log(res)
                },
                fail: (res) => {
                    console.log(res)
                }
            })
        },

        changeRemoteMotor(state) {
            wx.request({
                url: 'http://43.143.141.18:8080/api/v1/3U0fClbRjIwu5c5QxVkP/attributes',
                method: 'POST',
                data: {
                    "songSwitch":state,
                },
                success: (res) => {
                    console.log(res)
                },
                fail: (res) => {
                    console.log(res)
                }
            })
        },

        onChangeLED({ detail }) {
            wx.showModal({
                title: '提示',
                content: '是否切换LED灯状态？',
                success: (res) => {
                    if (res.confirm) {
                        this.setData({ checkedLED: detail });
                        console.log(detail)
                    }
                    if (this.data.checkedLED) {
                        this.setData({ LEDstatus: '开' });
                        this.changeRemoteLED(1);
                    } else {
                        this.setData({ LEDstatus: '关' });
                        this.changeRemoteLED(0);
                    }
                },
            });
        },
        onChangeMotor({ detail }) {
            wx.showModal({
                title: '提示',
                content: '是否切换电机状态？',
                success: (res) => {
                    if (res.confirm) {
                        this.setData({ checkedMotor: detail });
                    }
                    if (this.data.checkedMotor) {
                        this.setData({ Motorstatus: '开' });
                        this.changeRemoteMotor(1);
                    } else {
                        this.setData({ Motorstatus: '关' });
                        this.changeRemoteMotor(0);
                    }

                },
            });
        },
        setToken() {
            this.setData({
                token: app.globalData.token
            })
            console.log("this.data.toke")
            console.log(this.data.token)
        }
    }
});
